﻿; (function () {
    $('.clinic_container').height($(window).height() - 220)
    $('.center_left_container').css({ 'max-height': $(window).height() - 200 });
    var schedule = new Schedule("Center", $('#hdnRelativeUrl').val());
    schedule.days = 7;
    var technology = null;
    var page = {
        init: function () {
            $('#centerMainContain').height($(window).height() - 150);

            pageEvent.init();
        }
    };

    var pageEvent = {
        init: function () {
            //点击角标
            $('#mainContainSed').on('click', '.left_name', function () {
                var $this = $(this);
                $('.up_down').hide();
                $('.left_name').removeClass('left_name_open');

                $this.closest('.left_one').find('.up_down').show();
                $this.closest('.left_one').find('.left_name').addClass('left_name_open');
                
                var medRecNO = $this.closest('.left_one').data('medrecno');
                schedule.queryCondition.MedRecNO = medRecNO;
                if(!technology) {
                    technology = new Technology(schedule, {
                        backCallback: function(){
                            $.each(schedule.patientList, function(i, n){
                                if(n.MedRecNO == medRecNO){
                                    n.ExamRequestList = schedule.examRequestList;
                                    
                                    return false;
                                }
                            });
                        }});
                    technology.init();
                } else {
                    technology.init(true);
                }

                technology.clear();
                $('.center_right').show();
            });

            //搜索
            $('#search').click(function () {
                var carNo = $('#carNo').val();
                var patientClass = $('#patientClass').val();
                var applyTimeStart = $('#applyTimeStart').val();
                var applyTimeEnd = $('#applyTimeEnd').val();
                var scheduleFlag = $('.searchbar checked_bg').val();
                if (scheduleFlag == '1') {
                    scheduleFlag = '1';
                } else if (scheduleFlag == '0') {
                    scheduleFlag = '2';
                } else {
                    scheduleFlag = '0';
                }

                schedule.queryCondition = {
                    "MedRecNO": carNo,
                    "PatientClass": patientClass,
                    "RequestedDateBegin": applyTimeStart,
                    'RequestedDateEnd': applyTimeEnd,
                    'ScheduleFlag': scheduleFlag
                };

                schedule.getPatientExamRequestList(pageEvent.getPatientExamRequestListCallback);
            });
            //已预约未预约选择
            $("#searchBar").on('click', '.finish_check,.unfinish_check', function (ev) {
                var ev = ev || event;
                ev.stopPropagation();
                var $this = $(this);
                var isChecked = $this.prop('checked');
                if (isChecked) {
                    $this.addClass('checked_bg');
                } else {
               
                    $this.removeClass('checked_bg');
                }
             
            });
            //时间切换
            pageEvent.selectRequestedDate('applyTimeStart');
            pageEvent.selectRequestedDate('applyTimeEnd');
        },

        getPatientExamRequestListCallback: function (data) {
            var temp = ''
            $('.center_left_container').html('');
            $.each(data, function (i, n) {
                var border_bottom = ''
                if (i == data.length - 1) {
                    border_bottom = 'border_bottom';
                }
                temp += '<div class="left_one border_bottom" id="' + n.MedRecNO + '" data-medrecno="' + n.MedRecNO + '">' +
                            '<div class="left_name" >' +
                                '<div class="inpatient_name">' + 
                                    '<span>' +n.Name + '</span>' + 
                                    '<span style="padding-left:10px;">' +n.ExamRequestList[0].Sex + '</span>' + 
                                    '<span style="padding-left:10px;">' +n.ExamRequestList[0].Age + n.ExamRequestList[0].AgeUnit + '</span>' + 
                                '</div>' + 
                                '<div class="up_img">' + '（' + n.ExamRequestList.length + '）</div>' +
                            '</div>' +
                            '<div class="up_down">'+
                                pageEvent.appendPatientInfo(n)
                            +'</div>' +
                       ' </div>';

                $('.center_left_container').append(temp);
            });
        },

        appendPatientInfo: function (patient) {
            var temp = '';

            $.each(patient.ExamRequestList, function (j, v) {
                var border_bottom = '';
                if (j == patient.ExamRequestList.length - 1) {
                    border_bottom = 'border_bottom';
                }

                temp += '<div class="'+border_bottom+'">' + 
                            '<span class="sect">'+v.ServiceSectID+'</span>' + 
                            '<span style="padding-left:20px;">'+ v.ProcedureName +'</span>' + 
                        '</div>';
            });

            return temp;
        },

        //修改申请时间
        selectRequestedDate: function (id) {
            layui.use('laydate', function () {
                var laydate = layui.laydate;
                var start = {

                    max: '2099-06-16 23:59:59'
                      , istoday: false
                      , choose: function (datas) {
                      }
                };
                document.getElementById(id).onclick = function () {
                    start.elem = this;
                    laydate(start);
                }

            });
        }
    };

    page.init();

})();